class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if (!root)
        {
            return ans;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (q.size())
        {
            vector<int> tmp;
            int sz = q.size();
            for (int i = 0; i < sz; i++)
            {
                TreeNode* t = q.front();
                q.pop();
                tmp.push_back(t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
            ans.push_back(tmp);
        }
        return ans;
    }
};